/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.es.schema.impl;

import com.pingan.haofang.searchcloud.api.schema.IndexSchema;
import com.pingan.haofang.searchcloud.es.api.client.EsClientFactory;
import com.pingan.haofang.searchcloud.es.schema.TemplateManageService;
import com.pingan.haofang.searchcloud.es.schema.type.FieldMapping;
import com.pingan.haofang.searchcloud.es.schema.type.MetaFieldTypeMapper;
import com.pingan.haofang.searchcloud.es.schema.type.TypeFactory;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Template
 *
 * @author zhangbi617
 * @date 2017-07-05
 */
@Service
public class TemplateManageServiceImpl implements TemplateManageService {

    private static final String DEFAULT_SEARCH_FIELD_KEY = "default_search_field";
    private static final String SUGGESTION_KEY = "suggestion";

    private static FieldMapping DEFAULT_SEARCH_FIELD_MAPPING;
    private static FieldMapping SUGGESTION;

    static {
        DEFAULT_SEARCH_FIELD_MAPPING = new FieldMapping();
        DEFAULT_SEARCH_FIELD_MAPPING.setType(MetaFieldTypeMapper.TYPE_TEXT);
        DEFAULT_SEARCH_FIELD_MAPPING.setName(DEFAULT_SEARCH_FIELD_KEY);

        SUGGESTION = new FieldMapping();
        SUGGESTION.setName(SUGGESTION_KEY);
        SUGGESTION.setType(MetaFieldTypeMapper.TYPE_TEXT);
    }

    @Autowired
    private EsClientFactory esClientFactory;

    @Override
    public boolean create(String esHost, IndexSchema schema, String templateName) {
        TransportClient transportClient = esClientFactory.getByHost(esHost);
        PutIndexTemplateRequestBuilder builder = transportClient.admin().indices().preparePutTemplate(templateName);
        builder.setTemplate(templateName + "*");

        List<FieldMapping> fieldMappings = MetaFieldTypeConvertor.convert(schema);

        // COPY FIELD
        fieldMappings.add(DEFAULT_SEARCH_FIELD_MAPPING);
        fieldMappings.add(SUGGESTION);

        // 构建JSON
        try {
            // main
            XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().startObject();

            // --- properties
            mappingBuilder.startObject("properties");
            for (FieldMapping field : fieldMappings) {
                mappingBuilder.field(field.getName());
                mappingBuilder = TypeFactory.getType(field, mappingBuilder);
            }
            mappingBuilder.endObject();
            // --- properties end

            mappingBuilder.endObject();
            builder.addMapping(templateName, mappingBuilder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return builder.get().isAcknowledged();
    }

    @Override
    public boolean delete(String esHost, String templateName) {
        TransportClient transportClient = esClientFactory.getByHost(esHost);
        return transportClient.admin().indices().prepareDeleteTemplate(templateName).get().isAcknowledged();
    }
}
